package com.lepu.app.fun.lottery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;

import com.app.application.MyApplication;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.EmojiFilter;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.my.bean.BeanMyAddressManage;
import com.lepu.app.usercenter.bean.LoginInfo;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class AppLotteryInputActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener,AsyncRequest{

	private static final String REQUEST_GO_LOTTERY = "request_go_lottery";
	private static final String REQUEST_GET_ADDRESS = "request_get_address";
	
	private String mLinkTargetID = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_lottery_input_activity);

		init();
	}

	private void init() {
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("活动详情");
		topbar.setonTopbarNewLeftLayoutListener(this);
		
		Bundle data = getIntent().getExtras();
		if(data != null && data.size() >0)
		{
			mLinkTargetID = data.getString("LinkTargetID");
		}
		
		EditText userEditText = (EditText) findViewById(R.id.userEditText);
		EditText numberEditText = (EditText) findViewById(R.id.numberEditText);
		EditText addressEditText = (EditText) findViewById(R.id.addressEditText);
		
		userEditText.setFilters(new InputFilter[]{new EmojiFilter(),new InputFilter.LengthFilter(10)});
		numberEditText.setFilters(new InputFilter[]{new EmojiFilter(),new InputFilter.LengthFilter(17)});
        addressEditText.setFilters(new InputFilter[]{new EmojiFilter(),new InputFilter.LengthFilter(50)});//50

		Button lotteryButton = (Button) findViewById(R.id.lotteryButton);
		lotteryButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				requestSubmit();
			}
		});
		
		ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
		scrollView.setOnTouchListener(new OnTouchListener() {
			
			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				UtilityBase.closeSoftInput(AppLotteryInputActivity.this);
				return false;
			}
		});
		
		//requestData();
	}

	private void initData(ArrayList<BeanMyAddressManage> dataList)
	{
		if(dataList != null && dataList.size() > 0){
			BeanMyAddressManage beanMyAddressManage = null;
			for(BeanMyAddressManage item : dataList)
			{
				if(!TextUtils.isEmpty(item.IsDefault) && item.IsDefault.equals("1")){
					beanMyAddressManage = item;
					break;
				}
			}
			
			if(beanMyAddressManage != null)
			{
				EditText userEditText = (EditText) findViewById(R.id.userEditText);
				EditText numberEditText = (EditText) findViewById(R.id.numberEditText);
				EditText addressEditText = (EditText) findViewById(R.id.addressEditText);
				
				userEditText.setText(beanMyAddressManage.ReceiverName);
				numberEditText.setText(beanMyAddressManage.ReceiverPhone);
				addressEditText.setText(beanMyAddressManage.ReceiverAddress);
			}
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}
	
	@SuppressWarnings("unused")
	private void requestData() {

		showProgressDialog();
		
		String url = Setting.getMyAddressUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
		} catch (Exception e) {
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, this, REQUEST_GET_ADDRESS,
				true,false,true);
	}
	
	private void requestSubmit() {
		
		EditText userEditText = (EditText) findViewById(R.id.userEditText);
		EditText numberEditText = (EditText) findViewById(R.id.numberEditText);
		EditText addressEditText = (EditText) findViewById(R.id.addressEditText);
		
		String userText = userEditText.getText().toString().trim();
		String numberText = numberEditText.getText().toString().trim();
		String addressText = addressEditText.getText().toString().trim();
		
		if(TextUtils.isEmpty(userText))
		{
			UIHelper.showToast(this, "请您输入您的姓名");
			return ;
		}
		
		if(TextUtils.isEmpty(numberText))
		{
			UIHelper.showToast(this, "请您输入您的联系方式");
			return ;
		}
		
		if(numberText.length() < 7 || numberText.length() > 16)
		{
			UIHelper.showToast(this, "请您输入正确的联系方式");
			return ;
		}
		
		if(TextUtils.isEmpty(addressText))
		{
			UIHelper.showToast(this, "请您输入您的地址");
			return ;
		}
		
		String url = Setting.getLotteryUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();

		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);

			LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
			params.put("UserID", loginInfo.UserID);
			params.put("LoginToken", loginInfo.LoginToken);

			params.put("RaffleID", mLinkTargetID);
			
			params.put("ReceiverName", userText);
			params.put("ReceiverPhone", numberText);
			params.put("ReceiverAddress", addressText);
			
		} catch (Exception e) {
		}
		map.put("ht", params);

		showProgressDialog();
		ApiClient.http_post_main(url, map, null, this, REQUEST_GO_LOTTERY, true);
	}
	
	private void submitSuccess()
	{
		MyDialog myDialog = new MyDialog(this);
		myDialog.setMessage("感谢参与");
		myDialog.setNegativeButtonSplitImageHide(true);
		myDialog.setPositiveButton(R.string.app_ok, new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AppLotteryActivity.getInstance().updateLotteryState();
				finish(true);
			}
		});
		myDialog.create(null);
		myDialog.setCancelable(false);
		myDialog.setCanceledOnTouchOutside(false);
		myDialog.showMyDialog();
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		if(object.equals(REQUEST_GO_LOTTERY))
		{
			hideProgressDialog();
			
			submitSuccess();
		}else if(object.equals(REQUEST_GET_ADDRESS)){
			hideProgressDialog();
			
			ArrayList<BeanMyAddressManage> dataList = BeanMyAddressManage.parseJsonData((String)data);
			initData(dataList);
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if(object.equals(REQUEST_GO_LOTTERY))
		{
			hideProgressDialog();
		}else if(object.equals(REQUEST_GET_ADDRESS)){
			hideProgressDialog();
		}
	}

}
